<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@page import="com.google.gdata.client.authn.oauth.OAuthException"%>
<%@page import="com.google.gdata.client.authn.oauth.OAuthHmacSha1Signer"%>
<%@page import="com.google.gdata.client.authn.oauth.GoogleOAuthHelper"%>
<%@page import="com.google.gdata.client.authn.oauth.GoogleOAuthParameters"%>
<%@page import="com.google.appengine.api.users.User"%>
<%@page import="com.onewingu.gcs.server.TokenStore"%>
<%@page import="com.google.appengine.api.users.UserServiceFactory"%>
<%@page import="com.google.appengine.api.users.UserService"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>OneWingu Google Contacts Share</title>
</head>
<body>

<%
final String CONSUMER_KEY = "onewingu-gcontactshare.appspot.com";
final String CONSUMER_SECRET = "wXDf2McCoAsFRfV44iMpy25p";
final String OAUTH_CALLBACK = request.getRequestURL().toString();
final String CONTACTS_SCOPE = "https://www.google.com/m8/feeds/";

boolean isAuthorized = false;
UserService userService = UserServiceFactory.getUserService();
TokenStore ts = new TokenStore();
String accessToken = null;
if (!(userService.isUserLoggedIn())) {
%>
	Need to log in
<%
	String next = request.getRequestURL().toString();
	String loginURL = userService.createLoginURL(next);
%>
	<br /><a href="<%= loginURL %>">Login</a>
<%
} else {
	User user = userService.getCurrentUser();
	accessToken = ts.getToken(user.getEmail());
%>
	<br />logged in as <%= user.getEmail() %>
	<br />domain: <%= user.getAuthDomain() %>
	<br /> Administrator?: <%= userService.isUserAdmin() %>
<%

	if(accessToken == null) {
		GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
		oauthParameters.setOAuthConsumerKey(CONSUMER_KEY);
		oauthParameters.setOAuthConsumerSecret(CONSUMER_SECRET);
		oauthParameters.setScope(CONTACTS_SCOPE);
		oauthParameters.setOAuthCallback(OAUTH_CALLBACK);

		GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(new OAuthHmacSha1Signer());
		
		//check for requestToken
		oauthHelper.getOAuthParametersFromCallback(request.getQueryString(), oauthParameters);
		
		if(oauthParameters.getOAuthToken() == null || oauthParameters.getOAuthToken().isEmpty()) {
			//get unauthorized request token and provide authorization link
			oauthParameters.setOAuthToken("");
			oauthHelper.getUnauthorizedRequestToken(oauthParameters);
%>
	<p>getOAuthToken == <%= oauthParameters.getOAuthToken() %></p>
<%
			String approvalPageUrl = oauthHelper.createUserAuthorizationUrl(oauthParameters);
%>
			<a href="<%= approvalPageUrl %>">Authorize</a><br />
<%
		} else {
			//we should have an authorized request token now
			//upgrade to access token and persist to the datastore
%>
	<p>getOAuthToken == <%= oauthParameters.getOAuthToken() %></p>
<%
			try {
				accessToken = oauthHelper.getAccessToken(oauthParameters);
				String accessTokenSecret = oauthParameters.getOAuthTokenSecret();
				ts.addToken(user.getEmail(), accessToken, accessTokenSecret);
				isAuthorized = true;
			} catch(OAuthException oae) {}
		}
		
		
	} else {
		//we pulled the access token from the datastore
		isAuthorized = true;
%>
		<p>Already Authorized</p>
<%
	}
}

if(isAuthorized) {
%>
	<p>Now we can get down to business</p>
<%
}
%>

</body>
</html>